//
//  EmoticonController.m
//  StudentsOnlineTutoringSystem
//
//  Created by 袁量 on 2017/8/12.
//  Copyright © 2017年 袁量. All rights reserved.
//

#import "EmoticonController.h"
#import <AVFoundation/AVFoundation.h>
#import "EmoticonPackage.h"
#import "EmoticonViewCell.h"
static NSString *EmoticonCell = @"EmoticonCell";
@interface EmoticonController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIToolbar *toolBar;
@property (nonatomic, copy) void (^emoticonCallBack)(Emoticon *emoticon);
@end

@implementation EmoticonController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
    [self prepareForCollectionView];
    [self prepareForToolBar];
}
- (id)initWithEmoticonCallBack:(void(^)(Emoticon *emoticon)) emoticonCallBack{
    if (self = [super init]) {
        self.emoticonCallBack = emoticonCallBack;
    }
    return self;
}
#pragma mark ---界面设置---
- (void)setUpUI{
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.toolBar];
//    _collectionView.backgroundColor = [UIColor greenColor];
//    _toolBar.backgroundColor = [UIColor yellowColor];
    //设置控件frame
    _collectionView.translatesAutoresizingMaskIntoConstraints = false;
    _toolBar.translatesAutoresizingMaskIntoConstraints = false;
    NSDictionary *dict1 = NSDictionaryOfVariableBindings(_toolBar,_collectionView);
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_toolBar]-0-|" options:0  metrics:nil views:dict1]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_collectionView]-0-|" options:0  metrics:nil views:dict1]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_collectionView]-0-[_toolBar]-0-|" options:NSLayoutFormatAlignAllLeft & NSLayoutFormatAlignAllRight metrics:nil views:dict1]];
}

- (void)prepareForCollectionView{
     //注册cell、设置数据源
    [_collectionView registerClass:[EmoticonViewCell class] forCellWithReuseIdentifier:EmoticonCell];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 
    CGFloat itemWH = [[UIScreen mainScreen] bounds].size.width / 7;
    layout.itemSize = CGSizeMake(itemWH, itemWH);
    layout.minimumLineSpacing = 0.0;
    layout.minimumInteritemSpacing = 0.0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsVerticalScrollIndicator = false;
    self.collectionView.showsHorizontalScrollIndicator = false;
//    CGFloat insetMargin = (self.collectionView.bounds.size.height - 3 * itemWH) / 2;
    self.collectionView.contentInset = UIEdgeInsetsMake( 2,  0, 2, 0);
}
- (void)prepareForToolBar{
    NSArray *title = @[@"最近",@"默认",@"emoji",@"浪小花"];
    NSInteger index = 0;
    NSMutableArray *items = [NSMutableArray array];
    for (NSString *name in title) {
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:name style:UIBarButtonItemStylePlain target:self action:@selector(itemClick:)];
        item.tag = index;
        index ++;
        [items addObject:item];
        index <= title.count - 1 ? [items addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] : nil;
        
    }
    self.toolBar.items = items;
}
- (void)itemClick:(UIBarButtonItem *)item{
    NSInteger tag = item.tag;
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:tag];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
- (void)insertRecentlyEmoticon:(Emoticon *)emoticon{
    if (emoticon.isRemove || emoticon.isEmpty) {
        return;
    }
    EmoticonPackage *packages = self.manager.packages.firstObject;
    if ([packages.emoticons containsObject:emoticon]) {
        int index = (int)[packages.emoticons indexOfObject:emoticon];
        [packages.emoticons removeObjectAtIndex:index];
    }else{
        [packages.emoticons removeObjectAtIndex:19];
    }
    [packages.emoticons insertObject:emoticon atIndex:0];
}
#pragma mark ---代理方法---
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.manager.packages.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    return self.manager.packages.count;
    EmoticonPackage *package = self.manager.packages[section];
    return package.emoticons.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    EmoticonViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:EmoticonCell forIndexPath:indexPath];
    cell.backgroundColor = indexPath.item % 2 == 0 ? [UIColor darkGrayColor] : [UIColor whiteColor];
    
    EmoticonPackage *package = self.manager.packages[indexPath.section];
    Emoticon *emoticon = package.emoticons[indexPath.item];
    
    cell.emoticon = emoticon;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    EmoticonPackage *package = self.manager.packages[indexPath.section];
    Emoticon *emoticon = package.emoticons[indexPath.item];
//    NSLog(@"%@",emoticon);
    [self insertRecentlyEmoticon:emoticon];
    _emoticonCallBack(emoticon);
}

#pragma mark ---属性加载---
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
    return _collectionView;
}
- (UIToolbar *)toolBar{
    if (!_toolBar) {
        _toolBar = [[UIToolbar alloc] init];
    }
    return _toolBar;
}
- (EmoticonManager *)manager{
    if (!_manager) {
        _manager = [[EmoticonManager alloc] init];
    }
    return _manager;
}

@end
